package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.*;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IRoleService;
import com.yixing.tech.common.vo.s3.ApplicationVO;
import com.yixing.tech.common.vo.s3.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/role")
public class RoleController extends BaseControl {

    @Resource
    IRoleService roleService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody RoleVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        roleService.add(vo);
        return Rs.ok();
    }

    @PostMapping("/update/{roleId}")
    public Rs<Object> update(@RequestBody RoleVO vo,@PathVariable String roleId) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(roleId);
        vo.setTenantId(getTenantId());
        roleService.update(vo);
        return Rs.ok();
    }

    @PostMapping("/listPage")
    public Rs<Object> listPage(@RequestBody PageRow<RoleVO> vo) throws BusinessException {
        vo.getCondition(RoleVO.class).setTenantId(getTenantId());
        Page<RoleVO> page = roleService.list(vo);
        return Rs.ok(page);
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody RoleVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<RoleVO> list = roleService.list(vo);
        return Rs.ok(list);
    }

    @GetMapping("/{roleId}")
    public Rs<Object> get(@PathVariable String roleId) throws BusinessException {
        return Rs.ok(roleService.queryById(roleId,commonService.getApiUser()));
    }
    @GetMapping("/{roleId}/application/{appId}")
    public Rs<Object> get(@PathVariable String roleId,@PathVariable String appId) throws BusinessException {
        ApplicationVO cond = new ApplicationVO();
        cond.setId(appId);
        return Rs.ok(roleService.queryMenusByApp(roleId,cond,commonService.getApiUser()));
    }

    @DeleteMapping("/{roleId}")
    public Rs<Object> delete(@PathVariable String roleId) throws BusinessException {
        roleService.deleteById(roleId);
        return Rs.ok();
    }
}
